<?php
require_once("Interfaces/Controllable.php");
require_once("DAO/MySQL.php");
require_once("BO/Municipio.php");
$_DEBUG = true;
class  Municipios implements Controllable, Iterator{
public $_DEBUG = false;

    public $municipios ;
    public $index;
    public function __construct() {
       if($this->_DEBUG){
            echo "Construct Municipios";
        }
        $this->index = 0;
        $this->municipios = array();
    }

    function asControl($controlTag, $selected = false){
        $retVal = "";
        $selected = $this->current();
        for( $i = 0 ; $i < count($this->municipios) ; $i++ ){
            $retVal .= $this->municipios[$i]->asOption(( $this->municipios[$i] == $selected )) . "\n";
        }
        return "<$controlTag name='Municipios_idMunicipios' id='Municipios_idMunicipios'>".$retVal."</$controlTag>";
    }
    public function asSelect(){
        return $this->asControl("SELECT");
    }
    public function current (  ){
        if(count($this->municipios) > 0 )
        return $this->municipios[$this->index];
        return null;
    }
    public function key (  ){
        return $this->index;
    }
    public function next (  ){
        ++$this->index;
    }
    public function rewind (  ){
        $this->index = 0;
    }
    public function valid (  ){
        return isset($this->municipios[$this->index]);
    }
    public function loadAllFromDB(){
        $table = getTable("CALL ObtenerMunicipios(null, null, null);");    
        for( $i = 0 ; $i < count( $table ) ; $i++ ){
            $row = $table[ $i ];
            $municipio = new Municipio( $row["idMunicipios"] , $row["Codigo"] , $row["Municipio"] );
            $this->municipios[] = $municipio;
        }
    }
}
?>